import { axiosError, defaultErrorMessage } from 'src/util/errorHandler';
import http from 'src/util/http';
import qs from "qs"
import moment from 'moment'

type PostAuthSigninRequest = any
type PostAuthSigninResponse = any



const getAdGoods = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/getAdGoods?token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

const addMyAdGoods = async (dataParam: PostAuthSigninRequest) => {
  const data = qs.stringify(dataParam)
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/AddMyAdGoods`, { data });
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

// 我的广告
const getMyAdGoods = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/getMyAdGoods?FUserID=${data.FUserID}&FState=${data.FState}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};


// 读取我的交易明细
const getMyDetails = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/getMyDetails?FUserID=${data.FUserID}&FState=${data.FState}&Sdate=${moment(data.Sdate).format('YYYY-MM-DD HH:mm:ss')}&Edate=${moment(data.Edate).format('YYYY-MM-DD HH:mm:ss')}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

// 读取订单信息
const getMyOrder = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/getMyOrder?FUserID=${data.FUserID}&FState=${data.FState}&Sdate=${moment(data.Sdate).format('YYYY-MM-DD HH:mm:ss')}&Edate=${moment(data.Edate).format('YYYY-MM-DD HH:mm:ss')}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};
// 删除广告
const delMyAdGoods = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/DelMyAdGoods?FID=${data.FID}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};

// 更新为已查收放币
const updateOrder = async (data: PostAuthSigninRequest) => {
  try {
    return await http.post<PostAuthSigninResponse, PostAuthSigninRequest>(`Order/updateOrder?FID=${data.FID}&token=${data.token}`);
  } catch (e) {
    const error = axiosError(e);
    throw defaultErrorMessage(error);
  }
};
export default {
  getAdGoods,
  addMyAdGoods,
  getMyAdGoods,
  getMyDetails,
  getMyOrder,
  delMyAdGoods,
  updateOrder
};
